// pages/zhuanpan/zhuanpan.js
var choujiang = require('../../utils/util.js');
var ea = require('../../utils/each.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getFangjianProUrl: app.data.url +'/mapi/index/getproduct',
    getCoujiangUrl: app.data.url + '/mapi/index/getcoujiang',
    bijiaoUrl: app.data.url + '/mapi/newfenxiao/bijiao',
    xiqing: false,//抽奖产品详情是否隐藏
    yajin:0,  //押金 
    yuge:0,//余额
    kaichou:false,//开始抽奖的状态弹框
    chouzhongs:false,//抽中弹框状态 
    weichous:false, //未抽中弹框
    kaishicou:true,
    yue:false,

    chouzhong:{
      src:"https://db.wanquduo.cn/img/f2.png",
      name:'苹果手机'
    },
    fangjian:{},

    imgs: [{
      id:1,
      src: "https://db.wanquduo.cn/img/a3.png",
        state: true,
      },
      {
        id: 2,
        src: "https://db.wanquduo.cn/img/a3.png",
        state: false,
      },
      {
        id: 3,
        src: "https://db.wanquduo.cn/img/a3.png",
        state: false,
      },
      {
        id: 8,
        src: "https://db.wanquduo.cn/img/starts.png",
        state: false,
      },
      {
        id: 9,
        src: "https://db.wanquduo.cn/img/starts.png",
        state: false,
      },
      {
        id: 4,
        src: "https://db.wanquduo.cn/img/a3.png",
        state: false,
      },
      {
        id: 7,
        src: "https://db.wanquduo.cn/img/a3.png",
        state: false,
      },
      {
        id: 6,
        src: "https://db.wanquduo.cn/img/a3.png",
        state: false,
      },
      {
        id: 5,
        src: "https://db.wanquduo.cn/img/a3.png",
        state: false,
      },
    ],
    puid:[1,2,3,8,4,7,6,5],
    xiangqing: {
      src: 'https://db.wanquduo.cn/img/f2.png',
      name: '华为手机',
      jieshao: '华为P30全面屏超感光来卡三摄变焦双景录像980芯片智能手机....',
      guige: '126G',
      yuanjia: '￥3555',
      xianjia: '￥3600'
    },
    zhongjiang:{},
  },


  //点击产品详情
  xiqing: function(e) {
    // console.log(e);
    var uid = e.currentTarget.dataset.id;
    var that = this;
    // console.log(uid);
    if(uid==4){
      this.kaishi();
    }
    // else if(uid==8){
    //   return;
    // }
    else{
      var shuju = this.data.imgs;
      console.log(shuju[uid]);
      that.setData({ xiangqing:shuju[uid]});
      that.setData({ xiqing:true});
    }
  },


  //点击产品详情xx
  chacha: function() {
    this.setData({
      xiqing: false
    })
  },

  //点击抽中奖品xx
  chachajiangpin: function () {
    var that = this;
    var shuju = this.data.imgs;
    var newshuju = [];
    ea.each(shuju, function (i, v) {
      if (i == 0) {
        v.state = true;
      } else {
        v.state = false;
      }
      newshuju.push(v);
    })
    this.setData({ imgs: newshuju })
    this.setData({
      chouzhongs: false,//抽中弹框状态 
    })
  },

 //点击未抽中奖品xx
  chachaweichouzhong:function(){
    var that = this;
    var shuju = this.data.imgs;
    var newshuju = [];
    ea.each(shuju, function (i, v) {
      if (i == 0) {
        v.state = true;
      } else {
        v.state = false;
      }
      newshuju.push(v);
    })
    this.setData({ imgs: newshuju })
    this.setData({
      weichous: false,//抽中弹框状态 
    })

  },



 //点击开始抽奖
 kaishi:function(){
   var kaishicou = this.data.kaishicou;
   if (kaishicou){
     this.setData({ kaishicou:false});
     this.setData({ kaichou: true }); 
   }
  },

//取消抽奖
quxiaochou:function(){
  this.setData({ kaichou: false ,kaishicou:true}); 
},

  // 领取奖品
  lingqujiangpin: function () {
    this.setData({
      chouzhongs: false,//抽中弹框状态 
    })

    wx.switchTab({
      url: '/pages/jiangpin/jiangpin',
    })

  },
  // 继续抽奖 
  replay:function(){
    var that = this;
    var shuju = this.data.imgs;
    var newshuju = [];
    ea.each(shuju, function (i, v) {
      if (i == 0) {
        v.state = true;
      } else {
        v.state = false;
      }
      newshuju.push(v);
    })
    this.setData({ imgs: newshuju })
    that.setData({
      chouzhongs: false,//抽中弹框状态 
    })
  // 下面继续执行抽奖的方法


  },

  //点击再来一次 
  replaychoujiang: function () {
    var shuju = this.data.imgs;
    var newshuju = [];
    ea.each(shuju,function(i,v){
      if(i==0){
        v.state = true;
      }else{
        v.state = false;
      }
      newshuju.push(v);
    })
    this.setData({ imgs: newshuju})
    this.setData({
      weichous: false,//抽中弹框状态 
    })
    // 下面继续执行抽奖的方法
  
  },





// 缴纳押金抽奖
jiaona:function(){
  var kaichou = this.data.kaichou;
  if(!kaichou){
    return;
  }
  var that = this;
  var fangjian = this.data.fangjian;
  var rmb = parseFloat(fangjian.yajin);
  rmb = rmb.toFixed(2);
  var user = wx.getStorageSync('user');
  wx.request({
    url: that.data.bijiaoUrl,
    data:{token:user.token,rmb:rmb},
    method:'post',
    success:function(res){
      var data = res.data;
      if(data.code==200){
        that.zhencoujiang();
      }else{
        var shu = data.data;
        wx.requestPayment({
          timeStamp: shu.timeStamp,
          nonceStr: shu.nonceStr,
          package: shu.package,
          signType: shu.signType,
          paySign: shu.paySign,
          success:function(){
            setTimeout(function(){
              that.zhencoujiang();
            },500)
          }
        })
      }
    }
  })
},
  qucongz:function(){
    wx.navigateTo({
      url: '/pages/chongzhi/chongzhi',
    })
  },
zhencoujiang:function(){
  this.setData({ kaichou: false });
  var that = this;
  var user = wx.getStorageSync('user');
  var fangjian = that.data.fangjian;
  //开始执行抽奖的过程
  wx.request({
    url: that.data.getCoujiangUrl,
    method: 'post',
    data: { fangjianid: fangjian.id, token: user.token },
    success: function (res) {
      var data = res.data;
      // that.setData({ kaishicou: true });
      console.log(data);
      var msg = '';
      if (data.msg =="押金已退还"){
        msg = data.msg;
      }
      // return;
      if (data.code == 200) {
        console.log(data.data);
        var id = data.data.data.id;
        that.setData({ yuge: data.data.user.balance })
        console.log(id);
        var shuju = that.data.imgs;
        var zhong = 0;
        // console.log(shuju);
        // console.log(data.data);
        ea.each(shuju, function (i, v) {
          if (id == v.id) {
            that.setData({ zhongjiang: v });
            zhong = v.uid;
          }
        })
        var bushu = (Math.floor(Math.random() * 10) + 4) * 8 + zhong - 1;
        //开始抽奖
        
        choujiang.choujiang({ j_bushu: 10, j_times: 40, zhong: bushu }, function (str, bool) {
          const innerAudioContext = wx.createInnerAudioContext()
          innerAudioContext.autoplay = true
          innerAudioContext.loop = false;
          innerAudioContext.src = "/assets/f-18-7-12-05.wav";

          innerAudioContext.onPlay(() => {
            console.log('开始播放')
          })
          // setTimeout(function(){
          //   innerAudioContext.pause();
          // },200);

          var jiangping = that.data.imgs;
          var newjian = [];
          // console.log(str % 8);
          ea.each(jiangping, function (i, v) {
            if (str % 8 == (v.uid - 1)) {
              v.state = true;
            } else {
              v.state = false;
            }
            newjian.push(v);
          });
          that.setData({ imgs: newjian });
          if (bool) {
            // a表示判断是否中奖
            var jiangp = that.data.zhongjiang;
            console.log(jiangp);
            if (jiangp.zhongjiang == '未中奖') {
              var a = 1;
            } else {
              var a = 0;
            }
            if (a == 0) {
              that.setData({ chouzhong: jiangp });
              setTimeout(function () {
                that.setData({
                  chouzhongs: true,//抽中弹框状态 
                });
              }, 1000)
              if(msg=="押金已退还"){
                setTimeout(function(){
                  wx.showToast({
                    title: msg,
                  })
                },500)
              }
            } else {
              setTimeout(function () {
                that.setData({
                  weichous: true, //未抽中弹框
                })
              }, 1000);
            }
            that.setData({ kaishicou: true });
          }
        })



      } else if (data.code == 209) {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        });
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/chongzhi/chongzhi',
          })
        }, 1200)
      } else {
        that.setData({ kaishicou: true });
        wx.showToast({
          title: data.msg,
          icon: 'none'
        });
        that.setData({ kaishicou: true });
      }
    }
  })
},









  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var id = options.id;
    this.setData({id:id});
    var user = wx.getStorageSync('user');
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    console.log(user);
 wx.request({
   url: that.data.getFangjianProUrl,
   method:'post',
   data:{id:id,token:user.token},
   success:function(res){
     var data = res.data;
     if(data.code==200){
       console.log(data.data);
       if (data.data.coujiang.dir=="0"){
         that.setData({ yue:false})
       }else{
         that.setData({yue:true});
       }
       that.setData({ yajin: data.data.fangjian.yajin});
       that.setData({ fangjian: data.data.fangjian})
       that.setData({ yuge: data.data.user.balance});
       that.setData({guize:data.data.guize});
       var shuju = data.data.shuju;
       var puid = that.data.puid;
       var newshuju = [];
      //  console.log(shuju);
      //  return;
       ea.each(shuju,function(i,v){
         if(i==4){
           newshuju.push({
             fengmian_img:'https://db.wanquduo.cn/img/starts.png',
             src:'https://db.wanquduo.cn/img/starts.png',
             uid:9,
             id:0,
             title:'Start',
             xian_price:'开始'
           });
         }
           v.uid = puid[i];
           if(i==0){
             v.state = true;
           }else{
            v.state = false;
           }
           v.src = 'https://db.wanquduo.cn/img/a3.png';
         newshuju.push(v);
       })
       console.log(newshuju);
       that.setData({ imgs: newshuju});
       wx.hideLoading();
     }else{
       wx.showToast({
         title: data.msg,
         icon:'none'
       });
       setTimeout(function(){
         wx.navigateBack({
           delta: 1
         })
       },500);
     }
   }
 })

  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var id = this.data.id;
    this.onLoad({id:id});
    wx.stopPullDownRefresh({
      success: function () {
        console.log('返回了')
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})